<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>CentOS注册服务脚本 - macdfree&#39;s blog</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="macd" />
  <meta name="description" content="CentOS中服务脚本示例模板及注册方法
" />

  <meta name="keywords" content="blog, macd, even" />


<meta name="baidu-site-verification" content="Ak74Sk61bk" />
<meta name="google-site-verification" content="fAeFHpkrhfFS8LxdzXy0EtAPcVStv0faGn238sRF3L8" />


<meta name="generator" content="Hugo 0.54.0" />


<link rel="canonical" href="/wiki/linux/centos%E6%B3%A8%E5%86%8C%E6%9C%8D%E5%8A%A1%E8%84%9A%E6%9C%AC/" />

<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">




<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


<link href="/dist/even.min.css?v=3.1.1" rel="stylesheet">
<link href="/lib/fancybox/jquery.fancybox-3.1.20.min.css" rel="stylesheet">




<meta property="og:title" content="CentOS注册服务脚本" />
<meta property="og:description" content="CentOS中服务脚本示例模板及注册方法" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/wiki/linux/centos%E6%B3%A8%E5%86%8C%E6%9C%8D%E5%8A%A1%E8%84%9A%E6%9C%AC/" />
<meta property="article:published_time" content="2016-08-26T12:44:52&#43;00:00"/>
<meta property="article:modified_time" content="2016-08-26T12:44:52&#43;00:00"/>

<meta itemprop="name" content="CentOS注册服务脚本">
<meta itemprop="description" content="CentOS中服务脚本示例模板及注册方法">


<meta itemprop="datePublished" content="2016-08-26T12:44:52&#43;00:00" />
<meta itemprop="dateModified" content="2016-08-26T12:44:52&#43;00:00" />
<meta itemprop="wordCount" content="541">



<meta itemprop="keywords" content="bash," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="CentOS注册服务脚本"/>
<meta name="twitter:description" content="CentOS中服务脚本示例模板及注册方法"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">macdfree&#39;s blog</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/archive/">
        <li class="mobile-menu-item">归档</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">macdfree&#39;s blog</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/archive/">归档</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">CentOS注册服务脚本</h1>

      <div class="post-meta">
        <span class="post-time"> 2016-08-26 </span>
        <div class="post-category">
          
            <a href="/categories/linux/"> Linux </a>
          
        </div>
        <span class="more-meta"> 约 541 字 </span>
        <span class="more-meta"> 预计阅读 2 分钟 </span>
        <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
      </div>
    </header>

    
    
<div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  
  <div class="post-toc-content always-active">
    
  </div>
</div>

    
    <div class="post-content">
      <p>CentOS中服务脚本示例模板及注册方法</p>

<p>公司测试系统使用wrapper+tomcat部署，在CentOS环境上通过执行wrapper启动系统，但由于是在终端中执行，当退出终端后wrapper也会退出，所以想到将wrapper注册成系统服务。</p>

<!--more-->

<p>首先需要编写启动服务的脚本：</p>

<pre><code class="language-bash">#!/bin/bash

#description: 这是一个系统服务脚本
#chkconfig: 2345 20 81 

EXEC_PATH=/usr/local/          #需要注册的可执行文件所在文件夹的路径
EXEC=hello.sh                  #需要注册的可执行文件的文件名
DAEMON=/usr/local/hello.sh     #需要注册的可执行文件的完整文件名
PID_FILE=/var/run/hello.sh.pid #指定运行时的进程号

. /etc/rc.d/init.d/functions   #调用通用方法

if [ ! -x $EXEC_PATH/$EXEC ]; then
    echo &quot;ERROR: $DAEMON not found&quot;
    exit 1
fi

stop()
{
    echo &quot;Stoping $EXEC ...&quot;
    ps aux | grep &quot;$DAEMON&quot; | kill -9 `awk '{print $2}'` &gt;/dev/null 2&gt;&amp;1
    rm -f $PID_FILE
    usleep 100
    echo &quot;Shutting down $EXEC: [  OK  ]&quot;
}

start()
{
    echo &quot;Starting $EXEC ...&quot;
    $DAEMON &gt; /dev/null &amp;
    pidof $EXEC &gt; $PID_FILE
    usleep 100
    echo &quot;Starting $EXEC: [  OK  ]&quot;
}

restart()
{
    stop
    start
}

case &quot;$1&quot; in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status -p $PID_FILE $DAEMON
        ;;
    *)
        echo &quot;Usage: service $EXEC {start|stop|restart|status}&quot;
        exit 1
esac

exit $?
</code></pre>

<p>完成脚本编写后执行以下命令注册成服务</p>

<pre><code>chmod 700 test.sh             #设置脚本可执行
cp test.sh /etc/rc.d/init.d/  #拷贝脚本
chkconfig --add test.sh       #注册服务
chkconfig --list              #显示注册的服务
chkconfig  --del test.sh      #删除服务
</code></pre>

<p>注意点：
启动脚本中 <code>#description:</code> 和 <code>#chkconfig: 2345 20 81</code> 是必须的，否则执行 <code>chkconfig --add test.sh</code> 时会提示错误。</p>

<p>补充：
<code>#chkconfig</code>  中 <code>2345 20 81</code> 分别表示：</p>

<ul>
<li><p>2345表示服务的运行级别
0：停机
1：单用户模式
2：多用户，没有 NFS
3：完全多用户模式(标准的运行级)
4：没有用到
5：X11 多用户图形模式（xwindow)
6：重新启动</p></li>

<li><p>20表示启动的优先级</p></li>

<li><p>81表示关闭的优先级</p></li>
</ul>
    </div>

    
    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">macd</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">2016-08-26</span>
  </p>
  
  
</div>

    
    
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    
      <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/wechat-qr-code.png">
        <span>微信打赏</span>
      </label>
    
      <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/alipay-qr-code.png">
        <span>支付宝打赏</span>
      </label>
  </div>
</div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
          <a href="/tags/bash/">bash</a>
          
        </div>

      
      <nav class="post-nav">
        
          <a class="prev" href="/wiki/oracle/oracle%E5%AF%BC%E5%BA%93/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">Oracle导库</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        
          <a class="next" href="/wiki/python/python%E8%BD%AC%E7%A0%81%E9%97%AE%E9%A2%98%E7%9A%84%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95/">
            <span class="next-text nav-default">Python转码问题的解决方法</span>
            <span class="prev-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:macdfree@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/MACDfree" class="iconfont icon-github" title="github"></a>
  <a href="/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
    <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    
      2014 - 
    2019
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">macd</span>
    <span class="division">|</span>
    <span style="margin-left: 5px;">
      <img alt="buildstatus" src="https://travis-ci.org/MACDfree/hugoblog.svg?branch=master">
    </span>
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
<script src="/lib/highlight/highlight.pack.js?v=20171001"></script><script type="text/javascript" src="/lib/jquery/jquery-3.2.1.min.js"></script>
  <script type="text/javascript" src="/lib/slideout/slideout-1.0.1.min.js"></script>
  <script type="text/javascript" src="/lib/fancybox/jquery.fancybox-3.1.20.min.js"></script>
<script type="text/javascript" src="/dist/even.min.js?v=3.1.1"></script>








</body>
</html>
